-- 宝石属性tips

local jewelNodeNum = 4 -- 宝石孔个数
local panelOriginHeight = 0 -- 初始全尺寸高
local distanceOfTop = 0 -- 距离上边界距离
local distanceOfJewel = 0 -- 宝石间距
local jewelNodeHeight = 0 -- 宝石Node高

local jewelPropRender = import(".newJewelPropRender")

local newJewelPropView = class("newJewelPropView",function()
    return ccui.Layout:create()
end)

function newJewelPropView:ctor()
    local function createModuleNode(uiPath, uiNodeName, parent)
        local node = cc.uiloader:load( uiPath )
        local moduleNode = node:getChildByName( uiNodeName )
        moduleNode:retain()
        moduleNode:removeFromParent()
        parent:addChild( moduleNode )
        moduleNode:release()
        return moduleNode
    end

    self.mUINode = createModuleNode( Res.Tips_Jewel, "Panel", self )
    panelOriginHeight = self.mUINode:getContentSize().height
    self:setContentSize( self.mUINode:getContentSize() )
    ccui.Helper:doLayout( self.mUINode )

    self._jewelNodeList = {}
    for i = 1, jewelNodeNum do
        local nodeTemp = jewelPropRender.create( self.mUINode:getChildByName( "jewel_"..i ) )
        nodeTemp:setData()
        table.insert( self._jewelNodeList, nodeTemp )
    end

    local _,jewelNodeHeightTemp = self._jewelNodeList[1]:getContentSize2()
    jewelNodeHeight = jewelNodeHeightTemp
    distanceOfTop = panelOriginHeight - ( self._jewelNodeList[1]:getPositionY() + jewelNodeHeight )
    distanceOfJewel = self._jewelNodeList[1]:getPositionY() - ( self._jewelNodeList[3]:getPositionY() + jewelNodeHeight )
end

function newJewelPropView:refreshContent( jewelSlot )
    for i = 1, jewelNodeNum do
        self._jewelNodeList[ i ]:setData()
    end

    -- 填充宝石属性
    local index = 1
    for _, v in pairs( jewelSlot ) do
        self._jewelNodeList[ index ]:setData( v )
        index = index + 1
    end


    --刷新窗口高度
    local sizeWidth = self.mUINode:getContentSize2()
    local adjustedHeight = 0 -- 调整之后的高度
    if #jewelSlot <= 2 then
        adjustedHeight = panelOriginHeight - jewelNodeHeight - distanceOfJewel
    else
        adjustedHeight = panelOriginHeight
    end
    self.mUINode:setContentSize( cc.size( sizeWidth, adjustedHeight ) )

    -- 调整jewelNode PosY
    for i = 1, jewelNodeNum do
        local posYTemp = adjustedHeight - distanceOfTop - jewelNodeHeight*math.ceil(i/2) - distanceOfJewel*(math.ceil(i/2)-1)
        self._jewelNodeList[ i ]:setPositionY( posYTemp )
    end

    self:setContentSize( self.mUINode:getContentSize() )
    ccui.Helper:doLayout( self.mUINode )
end

return newJewelPropView
